Hugi Magazine 32: Say It With Flowers

hugi 32 header graphic

Demo Coding with Pure Basic #01 (Written By Fable Fox)

NOTE 1: This is a translation from Polaris of Northern Dragon's DevC++ demo coding tutorial from Hugi 31. So read it for a more in depth explanation. Since Polaris already explained the effects, I just tell how it works inside Pure Basic - that's it.

NOTE 2: No, I'm not a experienced demo coder. Although I've been involved since the days of Future Crew. Let's just say that only now I have had the time to code my own demos. So how did I start to learn? Studying Polaris' tutorial of course ;-)

NOTE 3: Polaris stated that he doesn't want to teach demo coding using Dark Basic because it's limiting and too slow. Thank god Pure Basic doesn't limit you, and it's FAST! (FASM anyone ;-P)

Who I Am

My real name is Azhar M.Z. I was known as Maniac Wolf before, until I had no money to pay for the .com, then someone else grabbed the domain. I was doing odd jobs just to have some food on the table, and was too tired to do anything else. There was another reason - the word 'Maniac' can't be used as a company name either. So I was thinking about a brand new name. I thought of the word Frozen Fox. I did a .com search, and no one was using it. So I worked harder to get extra money for the .com. By the time I had it, someone else had bought FrozenFox.com. So there I was flipping through the dictionary, looking for a cool word starting with the letter 'F'. I found 'Fable', it sounded cool, mixed well with Fox, and the meaning itself was great. Now I will be known as Fable Fox. But Maniac Wolf isn't dead. It is more alive than ever. No, I don't belong to any group. No releases yet. But I will be starting this year, I'm going to learn something - and make my dreams come true. And that is, to create demos.

Why I'm Doing This?

By doing this, I'm getting a lot of experience in programming, algorithms, math, graphics and sound. I plan to create a start-up game development and machinima house. I also read somewhere that 'you know nothing about something until you write about it'. Boy, he was right. I have a job of writing beginners books. Pick a topic, see if it sells, and write a 300+ pages book. The publisher will print 2000 copies. I received lump sum payment. It's cool. Suddenly, you will realize that you don't know a lot about something. It's not that you don't know how it works, but how to explain it sensibly. Sometimes, you will know one side (the one you like and know everything about) but not the other (the one you don't like and don't bother to read up on it). It's like you grew up on trackers, you know the tune, the effect and what not - but you don't even know what is a bar, clef, allegro, retard etc. It's like you can make a really cool tune, but you won't be writing musical notation. This is why I'm writing this tutorial. My second book was on Pure Basic. I'll be writing the basic, GUI and simple games. Luckily I've been in the demoscene for quite some time and have been programming for even longer time (GWBasic -> Turbo Pascal -> Turbo C++) as a hobby. Anyway, I need to know a lot, so I read a lot, asked a lot, and experimented a lot. Since the book is for beginners, I have to be above beginner level, and my experience and Diploma do help. So, have fun ;-)

NOTE: One might ask why I chose Pure Basic? Read my review of Pure Basic. I have included it with this article. Anyway, I have a license to Game Maker, Torque 3D, DX Studio and I plan to buy Torque 2D one day - it looks like my C/C++ days are practically over. Pure Basic is easy to use and multiplatform off the bat (as long as you use portable code/functions, that is). Fast, short code, easy to understand. Low price. If I want to build an app and I want to build it fast, Pure Basic is all up to it. I love programming, but I enjoy using the program that I write even more. Since Pure Basic allows me to arrive there faster, I'm happy with that.

The Great Setup.

What great setup? Just install Pure Basic and you're done ;-)

Beyond Breaking Ground...Hello World.

Since Polaris taught it, I guess I'll have to teach it too. Except that I'll give you three versions, console, GUI MessageBox and windowed - because Pure Basic is just too easy. Create a new source code and type the code below. Press F5 to run.

OpenConsole()
Print("World World!")
Input()
CloseConsole()

The first line OpenConsole() says that you want to create a console application - the one that is run inside a DOS Window. You must end the program with CloseConsole(). It's a function. Look, you obviously know this. If not, go read Pure Basic help files and some books on BASIC. Why? Even Polaris asked you to read some books on C/C++ ;-P Joke aside, the function Print("World World!") prints the string given to the screen. OOPS! Did I write World World in the code? Oh, a typo. Fix it yourself ;-)

The third line requests an Input from you - followed by a carriage return. The idea is that when you run the program, it will wait for you to press Enter instead of automatically exiting, making you unable to notice what happen as it will run too fast. Before you exit, you must call CloseConsole(). When you run it, it will appear to be like this:

If you just press F5, the window name is always PUREBASIC+numbers. If you want to give it a name, call this function after OpenConsole().

ConsoleTitle("Hello")

Unlike Polaris's tutorial, I'll add a bonus. A GUI "Hello World!". Why? It's easy to do it inside Pure Basic. Please don't blame Polaris, you have no idea how many line of code to make this happen in C/C++ under Win32. If you don't believe me go read Windows Game Programming Gurus. Yes, it's long, at least for beginners.

Create a new source file and type this. Press F5 to run.

MessageRequester("Info","Hello World !",0);

Just one line of code and you will get this:

Yes. The developer knows that most people nowdays want to create GUI applications, so they make it easy for you to do so. No, this one doesn't create a surface and prints on it, it's a MessageBox. (You'll know this if you read Win32 programming.) But that isn't important right now. I'll add that one too. Just to show how easy it is in Pure Basic ;-) Create a new source file and type the following code.

If OpenWindow(0, 0, 0, 320, 240, #PB_Window_SystemMenu, "App")
If StartDrawing(WindowOutput())
DrawText("Hello World!")
StopDrawing()
EndIf

Repeat : EventID = WaitWindowEvent() : Until EventID = #PB_Event_CloseWindow
EndIf

Press F5 to run it. You will get this.

Consult Pure Basic's help files to know what the code does, as I include it here for completion, not that it is important or anything. If I get around and explain it fully. Maybe in later articles, when we really get into this. Just to show how easy it is to write an application using Pure Basic.

NOTE: I'm not dissing C/C++. I know how to use it and I enjoy using it. But after I found Pure Basic, I'm a Pure Basic Person ;-)

NOTE: In later articles, you will find out that Pure Basic is able to use MiniFMOD, FMOD, Audiere, DX7 (built it), DirectX9, OpenGL and many other libraries out there. Pure Basic doesn't lock me from using many great libraries. So I doesn't feel limited, held down or handcuffed. Want more candy? Let's go watch The Matrix.

The Matrix Recoded

After 3 types of Hello World, let's get back to basic again. First, a loop. Simple loop.

OpenConsole()
Repeat

key_pressed$ = Inkey()
PrintN("Hello World!")

Until Left(key_pressed$, 1) = Chr(27)
CloseConsole()

What this code does is to keep on printing "Hello World!" until you press Esc. Now for the Matrix. Create a new source file, copy & paste this in, and run it.

OpenConsole() : Repeat : key_pressed$ = Inkey()
ConsoleLocate(Random(79),Random(22)+1)
odd = Random(1) : If odd = 0 : ConsoleColor(2,0) : Else : ConsoleColor(10,0) :EndIf
odd = Random(1) : If odd = 0 : Print("0") : Else : Print("1") : EndIf
Until Left(key_pressed$, 1) = Chr(27) : CloseConsole()

Short? Wow. Yes, 5 lines is all it needed. I know I put a lot on one line, but if you look at it, it isn't all that complex either. Yes, in Pure Basic, if you want to add more commands on one line you just use the character ':'. This is what you will get.

So you want the readable version, eh?

OpenConsole()

Repeat
key_pressed$ = Inkey()
ConsoleLocate(Random(79),Random(22)+1)
odd = Random(1)
If odd = 0
ConsoleColor(2,0)
Else
ConsoleColor(10,0)
EndIf
odd = Random(1)
If odd = 0
Print("0")
Else
Print("1")
EndIf
Until Left(key_pressed$, 1) = Chr(27)

CloseConsole()

This wasn't that long either. Sorry, it lost tab when I copied and pasted it. Just open the Pure Basic code.

NOTE: Random in Pure Basic range from 0 to the number you supplied. So Random(4) doesn't return either 0,1,2 or 3 but it will return either 0,1,2,3 or 4. In the code, Random(1) will return either 0 or 1.

NOTE: When you run it, you will notice it will run slower than Polaris's C/C++ code. But if you keep the spacebar or any key (expect Esc, of course) pressed, it will run as fast as Polaris's version. Why, I don't know. I might ask Pure Basic team regarding this, but knowing not many people care about realtime speed of console programs (it wasn't that slow either, just not as fast as the C/C++ version). I'm not sure if they will do anything about it.

NOTE: I doesn't draw on the first and last line.

Anyway, have fun. See you in the next article. Oh, by the way, I log on to the internet twice a week, one hour at a time. I use cybercafe PC, which means, I can't answer questions on the spot (especially if they are complex). Usually Tuesday and Saturday night. Right now I'm renting a room, and getting a broadband line is a real pain, plus wireless doesn't cover my area. Pray things go well by the end of this year as then I'm able to buy my own apartment. If you have any problems with Pure Basic, ask it at the Pure Basic forum. Any problem with this article, e-mail me. But don't hold your breath. Oh, pray that the spam blocker doesn't block your e-mail either. Rock.com is great. I rarely receive any spam, and nobody ever complains that they keep e-mailing me but didn't get any reply (sending an e-mail complaining logically serves no purpose - as it might get blocked too). Bye.

- Fable Fox, http://www.fablefox.com